package com.suqiu.qqrobot.listener;


import com.suqiu.qqrobot.config.properties.BotProperties;
import com.suqiu.qqrobot.config.properties.BotProperty;
import com.suqiu.qqrobot.config.AiSelector;
import com.suqiu.qqrobot.enums.AiTypeEnum;
import com.suqiu.qqrobot.enums.CommandTypeEnum;
import com.suqiu.qqrobot.model.Message;
import com.suqiu.qqrobot.service.AiChatService;
import lombok.RequiredArgsConstructor;
import love.forte.simboot.annotation.Filter;
import love.forte.simboot.annotation.Listener;
import love.forte.simbot.ID;
import love.forte.simbot.definition.Member;
import love.forte.simbot.event.FriendMessageEvent;
import love.forte.simbot.event.GroupMessageEvent;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * qq消息监听
 */
@Component
@RequiredArgsConstructor
public class MessageListener {

    private final AiSelector aiSelector;
    private final BotProperties botProperties;

    @Listener
    public void listener(FriendMessageEvent event) throws IOException {
        ID id = event.getFriend().getId();
        System.out.println("收到好友消息，好友id：" + id);
        String plainText = event.getMessageContent().getPlainText();
        AiChatService aiChatService = aiSelector.select(id.toString());
        Message<?> message = aiChatService.chat(id.toString(), plainText);
        message.reply(event);
    }


    @Listener
    @Filter(targets = @Filter.Targets(atBot = true))
    public void listener(GroupMessageEvent event) throws IOException {
        StringBuilder commandReplyMessage;
        ID id = event.getGroup().getId();
        ID ownerId = event.getGroup().getOwnerId();
        Member author = event.getAuthor();

        System.out.println("收到群消息，群id：" + id);
        String plainText = event.getMessageContent().getPlainText().substring(1);
        if (plainText.equals(CommandTypeEnum.CHOOSE_AI.getValue())) {
            if (verify(event, ownerId, author)) return;
            int i = 1;
            commandReplyMessage = new StringBuilder("请选择以下AI：\n\n");
            for (AiTypeEnum value : AiTypeEnum.values()) {
                commandReplyMessage.append(i++).append(".").append(value.getValue()).append("\n\n");
            }
            event.replyAsync(commandReplyMessage.toString());
            return;
        }
        AiTypeEnum aiTypeEnum = AiTypeEnum.of(plainText);
        if (aiTypeEnum != null) {
            if (verify(event, ownerId, author)) return;
            commandReplyMessage = new StringBuilder("你已选择该AI:\n" + aiTypeEnum.getValue());
            updateBotProperties(id.toString(), aiTypeEnum);
            event.replyAsync(commandReplyMessage.toString());
            return;
        }
        AiChatService aiChatService = aiSelector.select(id.toString());
        Message<?> message = aiChatService.chat(id.toString(), plainText);
        message.reply(event);
    }

    private boolean verify(GroupMessageEvent event, ID ownerId, Member author) {
        String commandReplyMessage;
        if (!ownerId.toString().equals(author.getId().toString())) {
            commandReplyMessage = "我很抱歉，你没有该权限，请联系伟大的群主";
            event.replyAsync(commandReplyMessage);
            return true;
        }
        return false;
    }

    private void updateBotProperties(String id, AiTypeEnum aiTypeEnum) {
        BotProperty botProperty = new BotProperty();
        botProperty.setType(aiTypeEnum);
        botProperties.put(id, botProperty);
    }

}
